package com.photo.tagger;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class SelectAPicActivity extends Activity implements OnClickListener {

	private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;
	private static final int OPEN_GALLERY_BROWSER_ACTIVITY_REQUEST_CODE = 2;
	private Uri fileUri;
	File file;

	/*
	 * @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO
	 * Auto-generated method stub return super.onCreateOptionsMenu(menu); }
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_pic);

		createInstances();
	}

	private void createInstances() {
		Button captureButton = (Button) findViewById(R.id.capture_button);
		Button chooseButton = (Button) findViewById(R.id.chooseButton);

		captureButton.setOnClickListener(this);
		chooseButton.setOnClickListener(this);

	}

	public void captureImage() {
		Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

		//file = new File(Environment.getExternalStorageDirectory()+ "/images/make_machine_example.jpg");
		//fileUri = Uri.fromFile(file);

		Intent intent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		//intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
		// create a file to save the image
		ContentValues values = new ContentValues(); 

		//cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the
		// image file name
		//fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
		// fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file
		// to save the image
		//cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the
																	// image
																	// file name

		// start the image capture Intent
		startActivityForResult(cameraIntent,
				CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
	}

	public void onClick(View b) {
		switch (b.getId()) {
		case R.id.chooseButton:
			openGallery();
			break;
		case R.id.capture_button:
			captureImage();
			break;

		}
	}

	public void openGallery() {
		// To open up a gallery browser
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(intent, "Select Picture"),
				1);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		Log.i("tag", "resulgfgdft");
		if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				Log.i("tag", "jkfhdkafnkadhfjh");

				// fileUri = data.getData();
				Log.i("tag", "fileUri kindha");

				Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
				Log.i("tag", "bitmap dhaka vachindhi");
				ImageView myImage = (ImageView) findViewById(R.id.backImage);
				Log.i("tag", "wooowwwwwwww");
				myImage.setImageBitmap(thumbnail);
				Log.i("tag", "malla fc ndku vasthundhi ddheenamma");

				// Image captured and saved to fileUri specified in the Intent

			}
		}
		if (requestCode == OPEN_GALLERY_BROWSER_ACTIVITY_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				fileUri = data.getData();
				showResult();
				// Image captured and saved to fileUri specified in the Intent

			}
		}

	}

	public void showResult() {
		file = new File(getRealPathFromURI(fileUri));
		if (file.exists()) {

			Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

			ImageView myImage = (ImageView) findViewById(R.id.backImage);
			myImage.setImageBitmap(myBitmap);

		}
	}

	public String getRealPathFromURI(Uri contentUri) {
		// can post image
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(contentUri, proj, // Which columns to
														// return
				null, // WHERE clause; which rows to return (all rows)
				null, // WHERE clause selection arguments (none)
				null); // Order-by clause (ascending by name)
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	private void sendResult() {
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
		byte[] b = baos.toByteArray();

		Intent intent = new Intent(this, TaggerActivity.class);
		intent.putExtra("picture", b);
		startActivity(intent);

	}

}
